import axios from 'axios';

const apiClient = axios.create({
  baseURL:  process.env.VUE_APP_BASE_URL,
  timeout: 10000,
});

const apiClientPhp = axios.create({
  baseURL:  process.env.VUE_APP_BASE_URL_PHP,
  timeout: 10000,
});


apiClient.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem("token");
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error); // 请求错误时 reject
  }
);

apiClient.interceptors.response.use(
  response => response.data,
  error => {
    if (error.response) {
      if (error.response.status === 0) {
        throw new Error('跨域请求被阻止，请检查服务器CORS配置');
      } else if (status === 401) {
        return Promise.reject(new Error('未授权，请重新登录'));
      }

      throw new Error(`请求失败: ${error.response.status}`);
    } else {
      console.error('Network Error:', error.message);
      throw error;
    }
  }
);

export default {
  login(params) {
    return apiClient.post('/edumanage/api/auth/login', params);
  },
  uploadAvatar(params) {
    return apiClient.post('/edumanage/api/avatar/upload', params);
  },
  update(params) {
    return apiClient.post('/edumanage/api/profile/update', params);
  },
  passreset(params) {
    return apiClient.post('/edumanage/api/reset', params);
  },
  //课程列表
  course_list(params) {
    return apiClient.post('/edumanage/api/course/page', params);
  },
  //根据ID查看课程
  get_course(params) {
    return apiClient.post('/edumanage/api/course/id', params);
  },
  //新增课程
  add_course(params) {
    return apiClient.post('/edumanage/api/course/add', params);
  },
  //更新课程
  update_course(params) {
    return apiClient.post('/edumanage/api/course/update', params);
  },
  //删除课程
  del_course(params) {
    return apiClient.post('/edumanage/api/course/del', params);
  },
  //发布课程
  publish_course(params) {
    return apiClient.post('/edumanage/api/course/publish', params);
  },
  //课程老师
  course_teachers(params) {
    return apiClient.post('/edumanage/api/courseRef/teachers', params);
  },
  //老师列表
  teacher_list(params) {
    return apiClient.post('/edumanage/api/teacher/page', params);
  },
  //保存课程-老师关系
  teacher_cocurse_save(params) {
    return apiClient.post('/edumanage/api/courseRef/save', params);
  },
  //保存老师账号
  teacher_save(params) {
    return apiClient.post('/edumanage/api/admin/addTeachers', params);
  },
  //用户分页
  user_list(params) {
    return apiClient.post('/edumanage/api/admin/pageUser', params);
  },
  //添加学生并创建账号
  add_student(params) {
    return apiClient.post('/edumanage/api/admin/addStudents', params);
  },
  //更新用户
  update_user(params) {
    return apiClient.post('/edumanage/api/admin/updateUser', params);
  },
  //根据id获取用户
  get_user(params) {
    return apiClient.post('/edumanage/api/admin/getUser', params);
  },
  //管理端-学科列表
  major_page(params) {
    return apiClient.post('/edumanage/api/major/page', params);
  },
  //管理端-学科添加
  major_add(params) {
    return apiClient.post('/edumanage/api/major/add', params);
  },
    //管理端-学科删除
  major_del(params) {
    return apiClient.post('/edumanage/api/major/del', params);
  },
  //管理端-学科详情
  major_detail(params) {
    return apiClient.post('/edumanage/api/major/id', params);
  },
  //管理端-学科更新
  major_update(params) {
    return apiClient.post('/edumanage/api/major/update', params);
  },
   //管理端-学科信息删除
  major_delInfo(params) {
    return apiClient.post('/edumanage/api/major/delInfo', params);
  },
  //老师端-习题列表
  exercise_page(params){
    return apiClient.post('/edumanage/api/exercise/page', params);
  },
  //老师端-习题保存
  exercise_addExercise(params){
    return apiClient.post('/edumanage/api/exercise/addExercise', params);
  },
    //老师端-习题详情
  exercise_detail(params){
    return apiClient.post('/edumanage/api/exercise/id', params);
  },
  //老师端-习题删除
  exercise_delete(params){
    return apiClient.post('/edumanage/api/exercise/del', params);
  },
  //老师端-课程列表
  course_page(params){
    return apiClient.post('/edumanage/api/courseRef/courses', params);
  },
    //老师端-课程详情
  course_detail(params){
    return apiClient.post('/edumanage/api/course/id', params);
  },
  //老师端-课程更新
  course_update(params){
    return apiClient.post('/edumanage/api/course/update', params);
  },
  //老师端-课程详情-课程目标
  course_courseTargetList(params){
    return apiClient.post('/edumanage/api/course/courseTargetList', params);
  },
  //老师端-课程详情-新增课程目标
  course_addCourseTarget(params){
    return apiClient.post('/edumanage/api/course/addCourseTarget', params);
  },
  //老师端-课程详情-更新课程目标
  course_updateCourseTarget(params){
    return apiClient.post('/edumanage/api/course/updateCourseTarget', params);
  },
   //老师端-课程详情-删除课程目标
  course_delCourseTarget(params){
    return apiClient.post('/edumanage/api/course/delCourseTarget', params);
  },
   //老师端-课程大纲列表
  course_graphList(params){
    return apiClientPhp.post('/api/Course/list', params);
  },
  //老师端-课程大纲添加
  course_graphAdd(params){
    return apiClientPhp.post('/api/Course/add', params);
  },
  //老师端-课程大纲更新
  course_graphUpdate(params){
    return apiClientPhp.post('/api/Course/edit', params);
  },
    //老师端-课程大纲删除
  course_graphDele(params){
    return apiClientPhp.post('/api/Course/dele', params);
  },
};